package com.yimaxiaoerlang.http;

import java.io.File;
import java.util.ArrayList;

import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

public class RetrofitHelper {

    public static RetrofitHelper getInstance() {
        return RetrofitHelper.SingletonHolder.sInstance;
    }

    private static class SingletonHolder {
        static RetrofitHelper sInstance = new RetrofitHelper();

        private SingletonHolder() {
        }
    }

    private RetrofitHelper() {

    }

    private String baseUrl = "https://im.yuefuximai.com";

    public void init(String baseUrl) {
        if (baseUrl == null || "".equals(baseUrl)) return;
        this.baseUrl = baseUrl;
    }


    private Retrofit mRetrofit;

    public Retrofit getRetrofit() {
        if (mRetrofit == null) {
            mRetrofit = new Retrofit.Builder().baseUrl(baseUrl)
                    .addConverterFactory(GsonConverterFactory.create())
                    .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                    .client(OkHttpClientHelper.getInstance().getOkHttpClient())
                    .build();
        }

        return mRetrofit;
    }

    public static <T> T getRequest(Class<T> clazz) {
        return RetrofitHelper.getInstance().getRetrofit().create(clazz);
    }


//    //文件转为可上传的文件流体
//    fun covertFile2Body(fileParamName: String, file:File): MultipartBody.Part {
//        //构建requestbodyotcet-stream
//        val requestFile = RequestBody.create(MediaType.parse("multipart/form-data;charset=UTF-8"), file)
////            val requestFile = RequestBody.create(MediaType.parse("application/otcet-stream"), file)
//        //将resquestbody封装为MultipartBody.Part对象
//        return MultipartBody.Part.createFormData(fileParamName, file.name, requestFile)
//    }

    //文件转为可上传的文件流体  单文件
    public static MultipartBody.Part covertFile2Body(String fileParamName, File file) {
        RequestBody requestFile = RequestBody.create(MediaType.parse("multipart/form-data;charset=UTF-8"), file);
        return MultipartBody.Part.createFormData(fileParamName, file.getName(), requestFile);
    }

    //多文件
    public static ArrayList<MultipartBody.Part> covertFiles2Body(String fileParamName, ArrayList<File> files) {
        ArrayList<MultipartBody.Part> parts = new ArrayList<>();
        for (File file : files) {
            RequestBody requestBody = RequestBody.create(MediaType.parse("multipart/form-data;charset=UTF-8"), file);
            MultipartBody.Part part = MultipartBody.Part.createFormData("files", file.getName(), requestBody);
            parts.add(part);
        }
        return parts;
    }

    //单文件
    public static MultipartBody filesToMultipartBody(File file) {
        MultipartBody.Builder builder = new MultipartBody.Builder();
        RequestBody requestBody = RequestBody.create(MediaType.parse(getType(file.getName())), file);
        builder.addFormDataPart("files", file.getName(), requestBody);
        builder.setType(MultipartBody.FORM);
        return builder.build();

    }

    private static String getType(String fileName) {
        if (fileName.endsWith("json")) {
            return "application/json";
        } else if (fileName.endsWith("xml")) {
            return "application/xml";
        }
        if (fileName.endsWith("png") || fileName.endsWith("PNG")) {
            return "image/png";
        }
        if (fileName.endsWith("jpg") || fileName.endsWith("JPG")) {
            return "image/jpeg";
        }
        if (fileName.endsWith("JPEG") || fileName.endsWith("jpeg")) {
            return "image/jpeg";
        }
        if (fileName.endsWith("gif")) {
            return "imge/gif";
        } else {
            return "multipart/form-data";
        }


//        json : application/json
//        xml : application/xml
//        png : image/png
//        jpg : image/jpeg
//        gif : imge/gif
    }
}

















